/**
 * 
 */
package wcg;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dialog.ModalityType;
import java.awt.Toolkit;

/**
 * @author ikechan
 *
 */
public class DlgChangeChannelID extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textFieldOriginal;
	private JTextField textFieldNew;
	private JTextField textFieldOffset;
	private JTabbedPane tabbedPane;
	
	public static int TypeOneByOne=0;
	public static int TYpeShiftAll=1;
	public int SpecifyType=-TypeOneByOne; 
	public int ChannelIDOriginal = -1;
	public int ChannelIDNew = -1;
	public int ChannelIDOffset = -1;
	public boolean Ok = false;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgChangeChannelID dialog = new DlgChangeChannelID();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DlgChangeChannelID() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(DlgChangeChannelID.class.getResource("/images/winchkg3-icon.png")));
		setModalityType(ModalityType.TOOLKIT_MODAL);
		setTitle("Change Channel ID");
		setBounds(100, 100, 450, 170);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setSelectedIndex(-1);
		
		contentPanel.add(tabbedPane, BorderLayout.NORTH);
		{
			JPanel panelOneByOne = new JPanel();
			tabbedPane.addTab("One by one", null, panelOneByOne, null);
			panelOneByOne.setLayout(new BorderLayout(0, 0));
			{
				JPanel panelC = new JPanel();
				panelOneByOne.add(panelC, BorderLayout.NORTH);
				{
					JLabel lblOriginalChannelId = new JLabel("Original Channel ID");
					panelC.add(lblOriginalChannelId);
				}
				{
					textFieldOriginal = new JTextField();
					panelC.add(textFieldOriginal);
					textFieldOriginal.setColumns(6);
				}
				{
					JLabel lblNewChannelid = new JLabel("New ChannelID");
					panelC.add(lblNewChannelid);
				}
				{
					textFieldNew = new JTextField();
					panelC.add(textFieldNew);
					textFieldNew.setColumns(6);
				}
			}
			{
				JLabel lblNewLabel = new JLabel("Specify channel ID hexadecimal notation.");
				panelOneByOne.add(lblNewLabel, BorderLayout.SOUTH);
			}
		}
		{
			JPanel panelOffsetAll = new JPanel();
			tabbedPane.addTab("Offset all", null, panelOffsetAll, null);
			panelOffsetAll.setLayout(new BorderLayout(0, 0));
			{
				JPanel panel = new JPanel();
				panelOffsetAll.add(panel);
				panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
				{
					JLabel lblOffsetValue = new JLabel("Offset value");
					panel.add(lblOffsetValue);
				}
				{
					textFieldOffset = new JTextField();
					panel.add(textFieldOffset);
					textFieldOffset.setColumns(10);
				}
			}
			{
				JLabel lblNewLabel_1 = new JLabel("Specify offset value hexadecimal. Each channle ID will be changed ID + Offset. ");
				panelOffsetAll.add(lblNewLabel_1, BorderLayout.SOUTH);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						getParam();
						if(SpecifyType!=-1){
							Ok = true;
						}
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	public void initDoSetParam(){
		if(ChannelIDOriginal != -1){
			String txt = new String(String.format("%x",ChannelIDOriginal));
			textFieldOriginal.setText(txt);
		}
		if(ChannelIDNew != -1){
			String txt = new String(String.format("%x",ChannelIDNew));
			textFieldNew.setText(txt);
		}
		if(ChannelIDOffset != -1){
			String txt = new String(String.format("%x",ChannelIDOffset));
			textFieldOffset.setText(txt);
		}
	
	}
	private void  getParam(){
		int sel = tabbedPane.getSelectedIndex();
		switch(sel){
		case 0: 
			SpecifyType = TypeOneByOne;
			try{
				ChannelIDOriginal = Integer.parseInt(textFieldOriginal.getText(),16);
				ChannelIDNew = Integer.parseInt(textFieldNew.getText(),16);
				
			}catch(NumberFormatException e){
				SpecifyType = -1;
			}
			break;
		case 1: 
			SpecifyType = TYpeShiftAll;
			try{
				ChannelIDOffset = Integer.parseInt(textFieldOffset.getText(),16);
			} catch(NumberFormatException e){
				SpecifyType = -1;
			}
 			break;
		default:
			SpecifyType = -1;
		
		}
	}
}
